package edu.chalmers.platypus.view.gui;

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.jdesktop.application.ResourceMap;

import edu.chalmers.platypus.util.ComBus;
import edu.chalmers.platypus.util.StateChanges;
import edu.chalmers.platypus.view.PlatypusGUI;

/**
 * Panel that shows the list of thumbnails and buttons
 */
public class BrowseViewPanel extends javax.swing.JPanel implements
	PropertyChangeListener {

    /** Constructor */
    public BrowseViewPanel() {
	initComponents();
	ComBus.subscribe(this);
    }

    /** Constructor */
    public BrowseViewPanel(PlatypusView parent) {
	this();
	this.parent = parent;
    }

    /** Sets the button that shows next panel (FilterViewPanel) to "Next" */
    public void setButtonNext() {
	ResourceMap resourceMap = org.jdesktop.application.Application
		.getInstance(edu.chalmers.platypus.view.gui.PlatypusApp.class)
		.getContext().getResourceMap(BrowseViewPanel.class);
	nextViewButton.setText(resourceMap.getString("nextViewButton.alttext"));
    }

    /** Sets the button that shows next panel (FilterViewPanel) to "Add filter" */
    public void setButtonAdd() {
	ResourceMap resourceMap = org.jdesktop.application.Application
		.getInstance(edu.chalmers.platypus.view.gui.PlatypusApp.class)
		.getContext().getResourceMap(BrowseViewPanel.class);
	nextViewButton.setText(resourceMap.getString("nextViewButton.text"));
    }

    /** Enables Add filter and Load preset buttons */
    public void enableNextButtons() {
	nextViewButton.setEnabled(true);
	loadPresetButton.setEnabled(true);
    }

    /** Disables Add filter and Load preset buttons */
    public void disableNextButtons() {
	nextViewButton.setEnabled(false);
	loadPresetButton.setEnabled(false);
    }

    /**
     * Below: autogenerated NetBeans code
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed"
    // desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

	nextViewButton = new javax.swing.JButton();
	batchThumbScrollPane = new javax.swing.JScrollPane();
	batchThumbPanel = new edu.chalmers.platypus.view.BatchThumbPanel();
	clearBatchButton = new javax.swing.JButton();
	loadPresetButton = new javax.swing.JButton();

	org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application
		.getInstance().getContext()
		.getResourceMap(BrowseViewPanel.class);
	setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap
		.getString("Form.border.title"))); // NOI18N
	setName("Form"); // NOI18N

	nextViewButton.setText(resourceMap.getString("nextViewButton.text")); // NOI18N
	nextViewButton.setEnabled(false);
	nextViewButton.setName("nextViewButton"); // NOI18N
	nextViewButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		nextViewButtonActionPerformed(evt);
	    }
	});

	batchThumbScrollPane.setBackground(resourceMap
		.getColor("batchThumbScrollPane.background")); // NOI18N
	batchThumbScrollPane
		.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
	batchThumbScrollPane.setName("batchThumbScrollPane"); // NOI18N

	batchThumbPanel.setName("batchThumbPanel"); // NOI18N
	batchThumbScrollPane.setViewportView(batchThumbPanel);

	clearBatchButton
		.setText(resourceMap.getString("clearBatchButton.text")); // NOI18N
	clearBatchButton.setName("clearBatchButton"); // NOI18N
	clearBatchButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		clearBatchButtonActionPerformed(evt);
	    }
	});

	loadPresetButton
		.setText(resourceMap.getString("loadPresetButton.text")); // NOI18N
	loadPresetButton.setEnabled(false);
	loadPresetButton.setName("loadPresetButton"); // NOI18N
	loadPresetButton.addActionListener(new java.awt.event.ActionListener() {
	    public void actionPerformed(java.awt.event.ActionEvent evt) {
		loadPresetButtonActionPerformed(evt);
	    }
	});

	javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
	this.setLayout(layout);
	layout.setHorizontalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			layout.createSequentialGroup()
				.addContainerGap()
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(
							batchThumbScrollPane,
							javax.swing.GroupLayout.DEFAULT_SIZE,
							463, Short.MAX_VALUE)
						.addGroup(
							javax.swing.GroupLayout.Alignment.TRAILING,
							layout.createSequentialGroup()
								.addComponent(
									clearBatchButton)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED,
									148,
									Short.MAX_VALUE)
								.addComponent(
									loadPresetButton)
								.addPreferredGap(
									javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(
									nextViewButton)))
				.addContainerGap()));
	layout.setVerticalGroup(layout
		.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
		.addGroup(
			javax.swing.GroupLayout.Alignment.TRAILING,
			layout.createSequentialGroup()
				.addComponent(batchThumbScrollPane,
					javax.swing.GroupLayout.DEFAULT_SIZE,
					222, Short.MAX_VALUE)
				.addPreferredGap(
					javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
				.addGroup(
					layout.createParallelGroup(
						javax.swing.GroupLayout.Alignment.BASELINE)
						.addComponent(nextViewButton)
						.addComponent(loadPresetButton)
						.addComponent(clearBatchButton))
				.addContainerGap()));

	batchThumbScrollPane.getViewport().setBackground(Color.WHITE);
	batchThumbScrollPane.getVerticalScrollBar().setUnitIncrement(20);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Next/Add filter button pressed. Next view (FilterViewPanel) shows if
     * there are filters already added, and shows Add Filter dialog if not.
     */
    private void nextViewButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_nextViewButtonActionPerformed
	if (parent != null) {
	    if (parent.numberOfFilterPanels() == 0) {
		parent.showAddFilterDialog();
	    }
	    if (parent.numberOfFilterPanels() > 0) {
		parent.showNextView();
	    }
	}
    }// GEN-LAST:event_nextViewButtonActionPerformed

    /** Load preset button pressed. Shows Load Preset dialog. */
    private void loadPresetButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_loadPresetButtonActionPerformed
	if (parent != null) {
	    parent.showLoadPresetDialog();
	}
    }// GEN-LAST:event_loadPresetButtonActionPerformed

    /** Clear batch button clicked. Calls controller to clear batch. */
    private void clearBatchButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_clearBatchButtonActionPerformed
	PlatypusGUI.getInstance().clearBatch();
    }// GEN-LAST:event_clearBatchButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private edu.chalmers.platypus.view.BatchThumbPanel batchThumbPanel;
    private javax.swing.JScrollPane batchThumbScrollPane;
    private javax.swing.JButton clearBatchButton;
    private javax.swing.JButton loadPresetButton;
    private javax.swing.JButton nextViewButton;
    // End of variables declaration//GEN-END:variables

    /** Reference to main window panel */
    private PlatypusView parent;

    /** Recieves info about backend operations */
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
	String change = evt.getPropertyName();
	if (change.equals(StateChanges.MODEL_RESET.toString())) {
	    // Filters and images cleared. Sets Next button to Add
	    setButtonAdd();
	    disableNextButtons();
	} else if (change.equals(StateChanges.NEW_IMAGE_IN_BATCH.toString())) {
	    // Image added to batch. Enables Add filter and Load preset buttons
	    enableNextButtons();
	} else if (change.equals(StateChanges.IMAGE_BATCH_EMPTY.toString())) {
	    // Image batch empty. Disables Add filter and Load preset buttons
	    disableNextButtons();
	} else if (change.equals(StateChanges.NEW_FILTER_ADDED_TO_BATCH
		.toString())) {
	    // New filter added to batch. Shows next view (FilterViewPanel)
	    // if this view is currently showing
	    if (this.isShowing()) {
		parent.showNextView();
	    }
	}
    }
}
